package bzapps.weynak;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RatingBar.OnRatingBarChangeListener;

public class Contactus extends Activity{
	
	Button send;
	EditText name,email,text;
	RatingBar rate;
	TextView ratetxt;
	String user = "christophe.zoghbi@bz-apps.com";
	String pass = "KrisSamer1234";
	String error, user_name,user_email,user_text;
	String user_rating;
	ProgressDialog pd;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.contactus);
		name = (EditText)findViewById(R.id.contact_name);
		email = (EditText)findViewById(R.id.contact_emailaddress);
		text = (EditText)findViewById(R.id.contact_emailtext);
		rate = (RatingBar)findViewById(R.id.contact_ratingbar);
		ratetxt = (TextView)findViewById(R.id.contact_ratetxt);
		send = (Button)findViewById(R.id.emailsendbutton);
		
		rate.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
			
			@Override
			public void onRatingChanged(RatingBar arg0, float rating, boolean arg2) {
				ratetxt.setText("Rating is :" + rating);
				user_rating = String.valueOf(rating);
			}
		});
		
		send.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
					user_name = name.getText().toString();
					user_email = email.getText().toString();
					user_text = text.getText().toString();
					
				if(user_name.equals("") || user_email.equals("") || user_text.equals(""))
				{
					 Toast.makeText(getBaseContext(), "Please insert all fields",10).show();
				}
				else {
				pd = ProgressDialog.show(Contactus.this, "", "Your feedback is being sent ... ",true,false);
				
				Thread thread = new Thread(){
					 public void run() {
					
								Mail m = new Mail(user, pass);
								String[] toArr = {"christophe.zoghbi@gmail.com","bejjanisamer@gmail.com"}; 
				           m.setTo(toArr);
				           m.setFrom(user);
				           m.setSubject("Weynak application feedback"); 
				           m.setBody("Name: " + user_name +"\n Email: " + user_email + "\n Text: " + user_text + "\n Rating: " + user_rating); 
				           
				       try {
				               if(m.send()) 
				               {
				            	 Log.v("WEYNAK.send.feedback", "Email sent!"); 
				            	 handler.sendEmptyMessage(0);
				               }
				             } catch(Exception e) { 
				            	 error = e.getMessage();
				             	handler.sendEmptyMessage(1);
				             	Log.e("WEYNAK.send.feedback", "Could not send email", e); 
				             }  
							}
				};
                thread.start();
				}
			}
		});
	}
	

	 private Handler handler = new Handler() {
         public void handleMessage(Message msg) {
        	 switch(msg.what){
        	 case 0:
        	     pd.dismiss();
        	     Toast.makeText(Contactus.this, "Thank you for your feedback :) ", Toast.LENGTH_LONG).show();
        	     finish();
        	 break;
        	 case 1:
        		 pd.dismiss();
        		 Toast.makeText(Contactus.this, "Could not send email "+ error, Toast.LENGTH_LONG).show();
        		 break;       			
        	
         }
      }
 };
}
